﻿package _A_TJTable;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
/**
 * 2011-6-26 2:04:24
 * @author Administrator
 * 演示在JTable中添加JButton
 */
public class JTable_CellRender_Button {
    JFrame frame;
    Container contentPane;
    JScrollPane jsp;
    JTable table;
    public JTable_CellRender_Button(){
        frame=new JFrame("表格组件测试");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane=frame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        String name[]={"姓名","语文","数学","合格"};
        Object data[][]={
            {"王鹏",91,100,"是"},
            {"李兵",99,89,"否"},
            {"肖龙",100,100,"是"}
        };
        DefaultTableModel dtm=new DefaultTableModel(data,name);
        dtm.setColumnCount(5);
        dtm.setRowCount(5);
        table=new JTable(dtm);
        //设置表格属性
        table.setRowHeight(50);
//        table.setRowHeight(4, 4*table.getRowHeight());
        table.setCellSelectionEnabled(true);
        table.setPreferredScrollableViewportSize(new Dimension(400,400));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        //设置列属性
        TableColumnModel tcm=table.getColumnModel();
        tcm.setColumnMargin(10);
        JButton bun=new JButton("是");
        bun.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JButton bun=(JButton)e.getSource();
                String str=bun.getText();
                if(str.equals("是")){
                    bun.setText("否");
                }else{
                    bun.setText("是");
                }
            }
        });
        tcm.getColumn(3).setCellEditor(new MyDefaultCellEditor(bun));
        DefaultTableCellRenderer dtcr=new DefaultTableCellRenderer(){
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                Component c=table.getCellEditor(row, column).getTableCellEditorComponent(table, null, isSelected, row, column);
                if(c instanceof JButton){
                    JButton cBun=(JButton)c;
                    Object text=table.getModel().getValueAt(row, column);
                    String str=(text==null)?"否":text.toString();
                    JButton bun=new JButton(str);
                    bun.setPreferredSize(cBun.getPreferredSize());
                    bun.setForeground(cBun.getForeground());
                    bun.setBackground(cBun.getBackground());
                    bun.setFont(cBun.getFont());
                    return bun;
                }
                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            }
        };
        for(int i=0;i<tcm.getColumnCount();i++){
            TableColumn tc=tcm.getColumn(i);
            tc.setPreferredWidth(100);
            tc.setCellRenderer(dtcr);
//            tc.setMinWidth(50);
        }
        jsp=new JScrollPane(table);
        contentPane.add(jsp,BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String args[]){
        new JTable_CellRender_Button();
    }
    class MyDefaultCellEditor extends AbstractCellEditor implements TableCellEditor {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        protected JComponent editorComponent;
        protected EditorDelegate delegate;
        protected int clickCountToStart = 1;
        public MyDefaultCellEditor(JButton button) {
            editorComponent = button;
            editorComponent.setRequestFocusEnabled(false);
            delegate = new EditorDelegate() {
                public void setValue(Object value) {
                    if(value==null){
                        ((JButton)editorComponent).setText("");
                    }
                    else{
                        String text=value.toString();
                        ((JButton)editorComponent).setText(text);
                    }
                }
                public Object getCellEditorValue() {
                    return ((JButton)editorComponent).getText();
                }
            };
        }
        public Component getComponent() {
            return editorComponent;
        }
        public void setClickCountToStart(int count) {
            clickCountToStart = count;
        }
        public int getClickCountToStart() {
            return clickCountToStart;
        }
        public Object getCellEditorValue() {
            return delegate.getCellEditorValue();
        }
        public boolean isCellEditable(EventObject anEvent) {
            return delegate.isCellEditable(anEvent);
        }
        public boolean shouldSelectCell(EventObject anEvent) {
            return delegate.shouldSelectCell(anEvent);
        }
        public boolean stopCellEditing() {
            return delegate.stopCellEditing();
        }
        public void cancelCellEditing() {
            delegate.cancelCellEditing();
        }
        public Component getTableCellEditorComponent(JTable table, Object value,
                                                     boolean isSelected,
                                                     int row, int column) {
            if(value==null){
                return editorComponent;
            }
            delegate.setValue(value);
            return editorComponent;
        }
        protected class EditorDelegate{
            protected Object value;
            public Object getCellEditorValue() {
                return value;
            }
            public void setValue(Object value) {
                this.value = value;
            }
            public boolean isCellEditable(EventObject anEvent) {
                if (anEvent instanceof MouseEvent) {
                    return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
                }
                return true;
            }
            public boolean shouldSelectCell(EventObject anEvent) {
                return true;
            }
            public boolean startCellEditing(EventObject anEvent) {
                return true;
            }
            public boolean stopCellEditing() {
                fireEditingStopped();
                return true;
            }
           public void cancelCellEditing() {
               fireEditingCanceled();
           }
        }
    }
}
